/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../../engine/control/XButton");
const { X2DBeta } = require("../../engine/graphics/X2DInterface");
const { XMultlineText } = require("../../engine/graphics/XMultlineText");
const { Scr } = require("../../engine/XDefine");
const { XTools } = require("../../engine/XTools");

class LogManager {
    static getInstance() {
        if (!LogManager.instance) {
            LogManager.instance = new LogManager();
        }
        return LogManager.instance;
    }
    static HIDE = true;
    static WIDTH = 480;
    static HEIGHT = 240;
    static LIMIT = 256;
    static loggers = [];
    static BRIGHTNESS = 0;
    static btnResize = new XButton({
        onDrag(dx, dy) {
            LogManager.WIDTH += dx;
            LogManager.HEIGHT -= dy;
        },
        onDraw() {
            let ox, oy;
            let r = 10;
            if (LogManager.HIDE) {
                ox = 5;
                oy = Scr.logich - 5 - r;
            } else {
                ox = LogManager.WIDTH + 5 - r;
                oy = Scr.logich - 5 - LogManager.HEIGHT;
            }
            LogManager.btnResize.move(ox, oy, r, r);
            if (LogManager.HIDE && LogManager.BRIGHTNESS > XTools.TIME_MS) {
                X2DBeta.fillRect(ox, oy, r, r, 0xff00ff00);
            }
            else {
                X2DBeta.drawRect(ox, oy, r, r, 0x80ff0000);
            }
        },
        onClicked() {
            LogManager.HIDE = !LogManager.HIDE;
        }
    })
    static expandArgs(...args) {
        let ss = ""
        for (let i = 0; i < args.length; i++) {
            let s;
            if (typeof args[i] === "object") {
                s = JSON.stringify(args[i]);
            } else {
                s = "" + args[i];
            }
            if (ss.length > 0) ss += "  ";
            ss += s;
        }
        ss = ss.split("\n");
        for (let i = 0; i < ss.length; i++) {
            if (ss[i].length > 256) {
                ss[i] = ss[i].substring(0, 256) + "...";
            }
        }
        return ss;
    }
    static I(...args) {
        let ss = this.expandArgs(...args);
        for (let s of ss) {
            LogManager.loggers.push([0, s]);
        }
        while (LogManager.loggers.length > this.LIMIT) {
            LogManager.loggers.shift();
        }
        if (LogManager.HIDE) {//闪烁提示
            LogManager.BRIGHTNESS = XTools.TIME_MS + 500
        }
    }
    static E(...args) {
        let ss = this.expandArgs(...args);
        for (let s of ss) {
            LogManager.loggers.push([1, s]);
        }
        while (LogManager.loggers.length > this.LIMIT) {
            LogManager.loggers.shift();
        }
        LogManager.HIDE = false;
    }
    static onDraw() {
        if (LogManager.HIDE) {
            LogManager.btnResize.onDraw();
            return;
        }
        X2DBeta.fillRect(5, Scr.logich - 5 - LogManager.HEIGHT, LogManager.WIDTH, LogManager.HEIGHT, 0x80400000);
        let offy = Scr.logich - 5;
        for (let i = LogManager.loggers.length - 1; i >= 0; i--) {
            let log = LogManager.loggers[i];
            let h = XMultlineText.height(log[1], 14, LogManager.WIDTH);
            offy -= h;
            if (offy < Scr.logich - 5 - LogManager.HEIGHT) {
                break;
            }
            if (log[0] == 0) {//info
                XMultlineText.draw(log[1], 14, LogManager.WIDTH, 5, offy, 1, 1, 0, 0, 0, 0xffe0e0e0);
            } else if (log[0] == 1) {//error
                XMultlineText.draw(log[1], 14, LogManager.WIDTH, 5, offy, 1, 1, 0, 0, 0, 0xffff0000);
            }
        }
        LogManager.btnResize.onDraw();
    }
    static onTouch(msg, x, y) {
        if (LogManager.btnResize.onTouch(msg, x, y)) {
            return true;
        }
        // if (msg != 2 && !LogManager.HIDE) {
        //     if (XTools.InRect(x, y, 5, Scr.logich - 5 - LogManager.HEIGHT, LogManager.WIDTH, LogManager.HEIGHT)) {
        //         return true;
        //     }
        // }
        return false;
    }
}

module.exports = {
    LogManager
}